﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NMLX_Data_Management
{
    public partial class Fqianc : Form
    {
        private ProjectDetail projectDetail;
        private int projectid;
        private int programid;
        private string projectname;
        private string programname;
        private string sPath;
        private string qcr;

        public Fqianc()
        {
            InitializeComponent();
        }

        public Fqianc(ProjectDetail projectDetail, int project, int program, string path, string projectname, string programname, string qcr)
        {
            InitializeComponent();
            this.projectDetail = projectDetail;
            this.projectid = project;
            this.programid = program;
            this.projectname = projectname;
            this.programname = programname;
            this.sPath = path;
            this.qcr = qcr;



        }

        private void Fqianc_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“nMLX_Data_MDataSet.User”中。您可以根据需要移动或删除它。
            this.userTableAdapter.Fill(this.nMLX_Data_MDataSet.t_User);
            label4.Text = projectname.ToString();
            label5.Text = programname.ToString();
            textBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Fqianc_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(comboBox1.Text))
            {
                return;
            }
            try
            {
                backgroundWorker1.RunWorkerAsync();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {            
            QcMethod();
        }
        public void QcMethod()
        {
            
            //如果程序已经签出
            if (!string.IsNullOrEmpty(this.qcr))
            {

                DialogResult dialogResult = MessageBox.Show(this,"该程序已经被" + this.qcr + "签出,是否要继续签出", "警告", MessageBoxButtons.OKCancel);
                if (dialogResult == DialogResult.OK)
                {
                    QiancMethod();
                }
                else
                {
                    this.Close();
                    return;
                }               
            }
            QiancMethod();

        }

        private void QiancMethod()
        {
            try
            {
                this.progressBar1.Value = 30;
                //File.Copy(this.sPath, textBox1.Text, true);
                FolderHelper.Copy(this.sPath, textBox1.Text, true);
                string sql = string.Format("update ProjectDetail set QCR='{0}',QCDate='{1}'where ProjectID='{2}' " +
                    "and ProjectDetail={3}", comboBox1.Text, DateTime.Now, this.projectid, this.programid);
                NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql);
                string sql1 = string.Format("insert into DetailRecord (username,datet,ProgramName,qcqr,ProjectId,ProgramId) " +
                    "values ('{0}','{1}','{2}','{3}','{4}','{5}') ", comboBox1.Text, DateTime.Now, "", "签出", this.projectid, this.programid);
                NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql1);
                this.progressBar1.Value = 100;
                MessageBox.Show(this,"签出成功，签出路径：" + textBox1.Text, "提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
           
        }

    }
}
